package org.linlinjava.litemall.app.interceptor;

import org.linlinjava.litemall.app.controller.BaseController;
import org.linlinjava.litemall.app.controller.GamchController;
import org.linlinjava.litemall.app.service.UserTokenManager;
import org.linlinjava.litemall.db.entity.Gamch;
import org.linlinjava.litemall.db.service.IGamchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商户操作拦截器
 */
public class MchHandlerInterceptor implements HandlerInterceptor {

    @Autowired
    private IGamchService gamchService;

    //目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      /*  //获取控制器的名字
        System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
        //获取方法名
        System.out.println(((HandlerMethod) handler).getMethod().getName());
*/
        if ("apply".equals(((HandlerMethod) handler).getMethod().getName())) {
            return true;
        }

        GamchController controller = (GamchController) ((HandlerMethod) handler).getBean();
        Gamch mch = gamchService.findByUid(controller.getUid());
        controller.setMch(mch);
        controller.setMchId(mch.getId());
        return true;
    }

}
